package com.jsmile.cloud.msgcenter.pay.manage;

import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.jsmile.cloud.accountcenter.api.feign.AccountServiceApi;
import com.jsmile.cloud.accountcenter.api.req.ReqMemberRegister;
import com.jsmile.cloud.authc.api.feign.UserServiceApi;
import com.jsmile.cloud.authc.api.req.ReqSysUserLogin;
import com.jsmile.cloud.authc.api.req.ReqSysUserRegister;
import com.jsmile.cloud.authc.api.res.ResSysUser;
import com.jsmile.cloud.msgcenter.api.exception.MsgException;
import com.jsmile.mail.security.handler.JsmileContext;
import com.jsmile.mail.security.kit.LoginKit;
import com.jsmile.mall.api.JSmileResult;
import com.jsmile.mall.api.enums.LoginTypeEnum;
import com.jsmile.mall.api.enums.UserTypeEnum;
import com.jsmile.mall.api.kit.StrKit;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.extra.spring.SpringUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
@AllArgsConstructor
public class WxLoginManage {

    private final RestTemplate restTemplate;

    // TODO 微信小程序授权登录
    public JSmileResult wxLogin(String openId, String appOpenId, String unionId, String nickName, String province, String city, String gender, String avatarUrl,
        JsmileContext context) {
        ReqSysUserRegister sysUserRegisterTo = new ReqSysUserRegister();
        sysUserRegisterTo.setTenantId(context.getTenantId());
        sysUserRegisterTo.setUserType(UserTypeEnum.WX_USER.getValue());
        sysUserRegisterTo.setUserName(StrKit.isBlank(unionId) ? openId : unionId);
        sysUserRegisterTo.setRealName(nickName);
        sysUserRegisterTo.setNickname(nickName);
        sysUserRegisterTo.setPhoto(avatarUrl);
        sysUserRegisterTo.setPassword(sysUserRegisterTo.getUserName());
        sysUserRegisterTo.setClientSource(context.getClientSource());
        JSmileResult<ResSysUser> result = SpringUtil.getBean(UserServiceApi.class).register(sysUserRegisterTo);
        if (!result.getResSuccess()) {
            throw new MsgException(result.getResCode(), result.getResMsg(), null);
        }
        ReqMemberRegister memberUpdateTo = new ReqMemberRegister();
        memberUpdateTo.setOpenId(openId);
        memberUpdateTo.setAppOpenId(appOpenId);
        memberUpdateTo.setUserId(result.getResData().getId());
        memberUpdateTo.setName(nickName);
        memberUpdateTo.setTenantId(context.getTenantId());
        memberUpdateTo.setProvince(province);
        memberUpdateTo.setCity(city);
        memberUpdateTo.setGender(gender);
        memberUpdateTo.setFaceLink(avatarUrl);
        SpringUtil.getBean(AccountServiceApi.class).register(memberUpdateTo);

        ReqSysUserLogin sysUserLoginTo = BeanUtil.toBean(result.getResData(), ReqSysUserLogin.class);
        sysUserLoginTo.setLoginType(LoginTypeEnum.NOPASSWORD.getValue());
        Map map = LoginKit.login(restTemplate, sysUserLoginTo);
        map.put("userInfo", result.getResData());
        return JSmileResult.success(map);
    }
}
